package cn.wolfcode.web.controller;


import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.feign.PayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * Created by lanxw
 */
@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private PayFeign payFeign;
    @RequestMapping("/pay")
    public Result<String> pay(String orderNo, Integer type){
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)){
//            在线支付
            return orderInfoService.payOnline(orderNo);
        }
        else {
            orderInfoService.payIntegral(orderNo);
//            积分支付
            return Result.success();
        }
    }
    @RequestMapping("/refund")
    public Result<String> refund(String orderNo){
        OrderInfo orderInfo = orderInfoService.findByOrderNo(orderNo);
        if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())){
//            在线支付
            orderInfoService.refundOnline(orderInfo);
        }
        else {
//            积分支付
            orderInfoService.refundIntergral(orderInfo);
        }
        return Result.success();
    }
//    异步回调
    @RequestMapping("/notify_url")
    String notifyUrl(@RequestParam Map<String ,String> params){
        Result <Boolean> result=payFeign.rsaCheckV1(params);
        if (result == null ||result.hasError()) {
            return "fail";
        }
        Boolean data = result.getData();
        if (data){
//            验证成功
            String orderNo = params.get("out_trade_no");
            int effectCount = orderInfoService.changePayStatus(orderNo, OrderInfo.STATUS_ACCOUNT_PAID, OrderInfo.PAYTYPE_ONLINE);
            if (effectCount==0){
//                退款
                System.out.println("修改失败，进行退款");
            }

        }
        return "success";
    }
    @Value("${pay.errorUrl}")
    private String errorUrl;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;
//    同步回调
    @RequestMapping("/return_url")
    public void returnUrl(@RequestParam Map<String ,String> params,HttpServletResponse response) throws IOException {
        System.out.println("同步回调接口");
        Result<Boolean> result = payFeign.rsaCheckV1(params);
        if (result == null || result.hasError()) {
            response.sendRedirect(errorUrl);
            return;
        }
        String orderNo = params.get("out_trade_no");
        response.sendRedirect(frontEndPayUrl+orderNo);
    }










}
